package com.softlook.mt.server.db.internal;

import java.util.List;
import java.util.Hashtable;
import java.util.Iterator;
import com.softlook.mt.server.db.dto.Place;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.LinkedList;
import com.softlook.mt.server.db.results.placemanager.GetResult;
import jp.crossfire.framework.database.ISelectResultListener;

public class PlaceManagerGetListner implements ISelectResultListener {
	public List result = new LinkedList();
	public Hashtable tables = new Hashtable();
	public List placeTmpCache = new LinkedList();
	public  PlaceManagerGetListner() {
		Hashtable hash = null;
		hash = new Hashtable();
		this.tables.put("place", hash);
		((Hashtable)this.tables.get("place")).put("userid", new Hashtable());
		((Hashtable)this.tables.get("place")).put("clientid", new Hashtable());
	}
	public void orMap() {
		Iterator it = this.result.iterator();
		while(it.hasNext()){
			// For each result record
			GetResult data = (GetResult)it.next();
			Place place = (Place)data.getPlace();
			if(place != null && !this.placeTmpCache.contains(place)){
				this.placeTmpCache.add(place);
			}
		}
		// CREATE FOREIGN KEY VALUE HASH
		// Setup references
	}
	public void getResult(ResultSet result, ResultSetMetaData meta) throws Exception {
		int maxCols = meta.getColumnCount();
		List tableColSettedList= new LinkedList();
		GetResult data = new GetResult();
		for(int i = 0; i < maxCols; i++){
			if(meta.getColumnName(i+1) == null){continue;}
		
			if(result.getObject(i+1) != null){
				data.put(meta.getColumnName(i+1).toUpperCase(), result.getObject(i+1));
			}
			String columnName = meta.getColumnName(i+1);
			if(Place.hasColumn(columnName)){
				if(columnName.toUpperCase().equals("placeid".toUpperCase())
		                   && !tableColSettedList.contains("place.placeid")){
					tableColSettedList.add("place.placeid");
					if(result.getObject(i+1) != null){
						data.getPlaceWithNew().setPlaceid(new java.lang.Integer(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("userid".toUpperCase())
		                   && !tableColSettedList.contains("place.userid")){
					tableColSettedList.add("place.userid");
					if(result.getObject(i+1) != null){
						data.getPlaceWithNew().setUserid(new java.lang.Integer(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("clientid".toUpperCase())
		                   && !tableColSettedList.contains("place.clientid")){
					tableColSettedList.add("place.clientid");
					if(result.getObject(i+1) != null){
						data.getPlaceWithNew().setClientid(new java.lang.Integer(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("lat".toUpperCase())
		                   && !tableColSettedList.contains("place.lat")){
					tableColSettedList.add("place.lat");
					if(result.getObject(i+1) != null){
						data.getPlaceWithNew().setLat(new java.lang.Double(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("lon".toUpperCase())
		                   && !tableColSettedList.contains("place.lon")){
					tableColSettedList.add("place.lon");
					if(result.getObject(i+1) != null){
						data.getPlaceWithNew().setLon(new java.lang.Double(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("delta".toUpperCase())
		                   && !tableColSettedList.contains("place.delta")){
					tableColSettedList.add("place.delta");
					if(result.getObject(i+1) != null){
						data.getPlaceWithNew().setDelta(new java.lang.Double(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("name".toUpperCase())
		                   && !tableColSettedList.contains("place.name")){
					tableColSettedList.add("place.name");
					if(result.getObject(i+1) != null){
						data.getPlaceWithNew().setName(new java.lang.String(result.getString(i+1)));
					}
					continue;
				}
			}
		}
		tableColSettedList.clear();
		this.result.add(data);
	}
}
